package com.itheima.service.impl;

import com.itheima.anno.MyLog;
import com.itheima.mapper.DeptMapper;
import com.itheima.mapper.EmpMapper;
import com.itheima.pojo.Dept;
import com.itheima.pojo.DeptLog;
import com.itheima.service.DeptLogService;
import com.itheima.service.DeptService;
import lombok.extern.java.Log;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;
import java.util.List;

@Service
public class DeptServiceImpl implements DeptService {
    @Autowired
    private DeptMapper deptMapper;
    @Autowired
    private EmpMapper empMapper;
    @Autowired
    private DeptLogService deptLogService;
    @Override
    public List<Dept> findAll() {
        return deptMapper.findAll();
    }

    @Override
    @MyLog
    public void deleteById(Integer id) {
        DeptLog deptLog = new DeptLog();
        deptLog.setCreateTime(LocalDateTime.now());
        deptLog.setDescription("删除id="+id+"的部门");

        try {
            deptMapper.deleteById(id);
            empMapper.deleteByDeptId(id);
            deptLog.setResult(1);
        } catch (Exception e){
            deptLog.setResult(0);
            e.printStackTrace();
            throw e;
        }finally {
            deptLogService.insert(deptLog);
        }
    }

    @Override
    public void add(Dept dept) {
        dept.setUpdateTime(LocalDateTime.now());
        dept.setCreateTime(LocalDateTime.now());
        deptMapper.add(dept);
    }

    @Override
    public Dept findById(Integer id) {
        Dept list=deptMapper.findById(id);
        return list;
    }

    @Override
    public void update(Dept dept) {
        deptMapper.update(dept);
    }
}
